package com.newangels.action;

import com.newangels.service.AgentManagerService;
import com.newangels.service.impl.AgentManagerServiceImpl;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * @author: TangLiang
 * @date: 2020/5/21 17:16
 * @since: 1.0
 */
@WebServlet("/updateAgent")
public class UpdateAgentServlet extends HttpServlet {
    AgentManagerService agentManagerService = AgentManagerServiceImpl.getAgentManagerServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/plain");
        String AGENT_ID_ = request.getParameter("AGENT_ID_");
        String AGENT_CODE_ = request.getParameter("AGENT_CODE_");
        String AGENT_NAME_ = request.getParameter("AGENT_NAME_");
        String AGENT_URL_ = request.getParameter("AGENT_URL_");
        String AGENT_ARGUMENTS_ = request.getParameter("AGENT_ARGUMENTS_");
        Integer AGENT_COST_ = request.getParameter("AGENT_COST_") == null || request.getParameter("AGENT_COST_").length() == 0 ? null : Integer.parseInt(request.getParameter("AGENT_COST_"));
        String AGENT_SOURCE_ = request.getParameter("AGENT_SOURCE_");
        JSONObject jsonObject = new JSONObject();
        if (agentManagerService.updateAgent(AGENT_ID_, AGENT_CODE_, AGENT_NAME_, AGENT_URL_, AGENT_ARGUMENTS_, AGENT_COST_, AGENT_SOURCE_) == 1) {
            jsonObject.put("success", true);
            HashMap<String, Object> map = new HashMap<String, Object>(){
                {
                    put("AGENT_CODE_", AGENT_CODE_);
                    put("AGENT_NAME_", AGENT_NAME_);
                    put("AGENT_URL_", AGENT_URL_);
                    put("AGENT_ARGUMENTS_", AGENT_ARGUMENTS_);
                    put("AGENT_COST_", AGENT_COST_);
                    put("AGENT_SOURCE_", AGENT_SOURCE_);
                }
            };
            jsonObject.put("result", map);
        } else {
            jsonObject.put("success", false);
        }
        response.getWriter().write(jsonObject.toString());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
